package com.kecheng.action;

import com.kecheng.dao.CommonDao;
import com.kecheng.moban.PageBean;
import com.kecheng.pojo.TiMu;
import com.kecheng.pojo.TmOption;
import com.kecheng.pojo.TmSource;
import com.kecheng.util.FileUtil;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.AllowedMethods;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
 * 后台用户模块
 *
 * @author
 */

@Action(value = "timu", results = {
        @Result(name = "toDetail", location = "/admin/timu/detail.jsp"),
        @Result(name = "toUpdate", location = "/admin/timu/update.jsp"),
        @Result(name = "redirectList", type = "redirect",location = "timu!daTiList.action"),
        @Result(name = "datiList", location = "/admin/timu/datiList.jsp"),
})
@AllowedMethods(value = {("regex:.*")})
public class TimuAction extends BaseAction {

    @Autowired
    CommonDao dao;

    private TiMu timu;
    private List<TiMu>tiMuList;
    private TmSource tmSource;
    private File file;
    private String fileFileName;
    private List<Integer> id;
    private List<String>optionContent;

    private PageBean<TmSource> pageBean;

    /**
     * 大题列表
     * @return
     */
    public String daTiList(){
        if(tmSource==null){
            tmSource = new TmSource();
            tmSource.setType("1");
        }
        pageBean = (PageBean<TmSource>) dao.findPageBeanByObj(tmSource,pageBean);
        return "datiList";
    }

    /**
     *详情页
     * @return
     */
    public String toDetail(){
        if(tmSource!=null){
            tmSource = (TmSource)dao.findOne(tmSource);

            //查找题目
            timu = new TiMu();
            timu.setParentId(tmSource.getId());
            tiMuList = (List<TiMu>)dao.find(timu);
            for(TiMu tm:tiMuList){
                TmOption option = new TmOption();
                option.setTmId(tm.getId());
                List<TmOption>optionList = (List<TmOption>)dao.find(option);
                tm.setTmOptionList(optionList);
            }
//            for (TmOption tmo:optionList){
//                optionContent.add(tmo.getContent());
//            }
            return "toUpdate";
        }
        return "toDetail";
    }
    /**
     * 更新或者添加
     */
    @Transactional(readOnly = false)
    public String save(){
        TmOption option = null;
        if(file!=null){
            tmSource.setVoiceUrl(FileUtil.pictureUtil(file,fileFileName,root));
        }
        //增加修改大题
        if(tmSource.getId()!=null){
            //更新的时候，先删除小题，选项，在重新增加
            deleteTm(tmSource.getId());

            dao.update(tmSource);
        }else{
            dao.add(tmSource);
        }
        //增加小题
        for (TiMu tm:tiMuList){
            tm.setQuestionType(tmSource.getQuestionType());
            tm.setParentId(tmSource.getId());
            dao.add(tm);
            //增加选项
            for(int i=0;i<4;i++){//此处有一个问题，会出现多个空值，所以暂时写死，等后面改的时候再说
                option = new TmOption();
                option.setContent(optionContent.get(i));
                option.setOptionType(i+1);
                option.setTmId(tm.getId());
                option.setDaTiId(tmSource.getId());
                dao.add(option);
            }
            optionContent = optionContent.subList(4,optionContent.size());//已经用过的选项去除掉
        }

        return "redirectList";
    }

    /**
     *删除
     * @return
     */
    @Transactional(readOnly = false)
    public String delete(){
        if(tmSource!=null){
            deleteTm(tmSource.getId());
            dao.delete(tmSource);
        }
        else if(id!=null){
            for(Integer i:id){
                deleteTm(i);//删除小题以及选项
                tmSource = new TmSource();
                tmSource.setId(i);
                dao.delete(tmSource);
            }
        }
        return "redirectList";
    }

    /**
     * 删除大题中的小题，以及选项
     * @param tmSourceId
     */
    private void deleteTm(Integer tmSourceId){
        timu = new TiMu();
        timu.setParentId(tmSourceId);
        List<TiMu>tiMus = (List<TiMu>)dao.find(timu);
        dao.dedete(tiMus);

        //删除选项
        TmOption option = new TmOption();
        option.setDaTiId(tmSourceId);
        List<TmOption>optionList = (List<TmOption>)dao.find(option);
        dao.dedete(optionList);
    }

    public TiMu getTimu() {
        return timu;
    }

    public void setTimu(TiMu timu) {
        this.timu = timu;
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public String getFileFileName() {
        return fileFileName;
    }

    public void setFileFileName(String fileFileName) {
        this.fileFileName = fileFileName;
    }

    public List<Integer> getId() {
        return id;
    }

    public void setId(List<Integer> id) {
        this.id = id;
    }

    public List<String> getOptionContent() {
        return optionContent;
    }

    public void setOptionContent(List<String> optionContent) {
        this.optionContent = optionContent;
    }

    public TmSource getTmSource() {
        return tmSource;
    }

    public void setTmSource(TmSource tmSource) {
        this.tmSource = tmSource;
    }

    public PageBean<TmSource> getPageBean() {
        return pageBean;
    }

    public void setPageBean(PageBean<TmSource> pageBean) {
        this.pageBean = pageBean;
    }

    public List<TiMu> getTiMuList() {
        return tiMuList;
    }

    public void setTiMuList(List<TiMu> tiMuList) {
        this.tiMuList = tiMuList;
    }
}
